package com.qyhl.webtv.module_news.news.information.program;

import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.news.NewsBean;
import com.qyhl.webtv.commonlib.entity.news.SectionBean;
import com.qyhl.webtv.module_news.common.NewsUrl;

import java.util.List;

/**
 * Created by qyhl on 2017/11/29.
 */

public class ProgramModel implements ProgramContract.ProgramModel {
    private ProgramPresenter mPresenter;

    public ProgramModel(ProgramPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    /**
     * 获取节目列表
     *
     * @param menuId
     */
    @Override
    public void getSection(String menuId,String type) {

        EasyHttp.get(NewsUrl.GET_MENU_LIST)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("siteId", String.valueOf(CommonUtils.getInstance().getSite()))
                .params("menuId", menuId)
                .params("menuType", type)
                .execute(new SimpleCallBack<List<SectionBean>>() {

                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setError(2, "暂无任何内容！");
                        } else {
                            mPresenter.setError(1, "解析出错，加载失败！");
                        }
                    }

                    @Override
                    public void onSuccess(List<SectionBean> sectionBeans) {
                        if (sectionBeans != null && sectionBeans.size() > 0) {
                            mPresenter.setSection(sectionBeans);
                        } else {
                            mPresenter.setError(2, "暂无任何内容！");
                        }
                    }
                });

    }

    /**
     * 获取最新推荐
     *
     * @param section
     */
    @Override
    public void getHotNews(String section) {

        EasyHttp.get(NewsUrl.GET_UPTODATE_NEWS)
                .params("tagName", CommonUtils.getInstance().getSiteName())
                .params("siteId", String.valueOf(CommonUtils.getInstance().getSite()))
                .params("catalogID", section)
                .execute(new SimpleCallBack<List<NewsBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.newsError();
                    }

                    @Override
                    public void onSuccess(List<NewsBean> newsBeans) {
                        if (newsBeans != null && newsBeans.size() > 0) {
                            mPresenter.setNews(newsBeans);
                        } else {
                            mPresenter.newsError();
                        }
                    }
                });
    }
}
